<?php

namespace app\batch;

use think\console\Command;
use think\console\Input;
use think\console\Output;
use app\models\{User as Users, Angts, Quotes, Order};
use think\Facade\Config as Configs;
use think\Db;
use app\facade\Redis;
use think\facade\Debug;


class Coinquote extends Command
{
    protected function configure()
    {
        $this->setName('Coinquote')->setDescription('系统币行情跑数据');
    }

    protected function execute(Input $input, Output $output)
    {
        $this->doCron();
        die();
    }

    protected function doCron()
    {
        $cmd = "ps aux | grep '/www/server/php/72/bin/php /www/wwwroot/JTE/think Jtequotes'|grep -v 'grep'| awk '{print $2}'";
        exec($cmd, $ps, $ret);
        if (count($ps) > 1) {
            die("batch of  is running");
        }
        ini_set('max_execution_time', 0);
        $quotes = (new Quotes())->where('ishow', 1)->field('id,coin,mart')->select();
        $size = 150; //数据长度
        $lists = [];
        foreach ($quotes as $quote) {
            $coin = Configs::get('config.coins.' . $quote['coin']);
            $mart = Configs::get('config.martcoins.' . $quote['mart']);
            if ($coin == 1) {
                $lists = $quote;
            }
        }
        if (empty($lists)) {
            return false;
        }
        $types = [1, 5, 15, 30, 60, 24];
        foreach ($types as $type) {
            if ($type == 1) {
                $time = 1 * 60;
                $rdstimes = 60;
            } elseif ($type == 5) {
                $time = 5 * 60;
                $rdstimes = 60;
            } elseif ($type == 15) {
                $time = 15 * 60;
                $rdstimes = 180;
            } elseif ($type == 30) {
                $time = 30 * 60;
                $rdstimes = 5 * 60;
            } elseif ($type == 60) {
                $time = 60 * 60;
                $rdstimes = 600;
            } elseif ($type == 1440) {
                $time = 24 * 60 * 60;
                $rdstimes = 3 * 3600;
            } else {
                $time = 0;
                $rdstimes = 0;
            }
            $rdscoinmart = 'coinquote' . $lists['id'] . 'type_' . $type;
            $valuerds = Redis::get($rdscoinmart); //获取值
            $timerds = Redis::ttl($rdscoinmart);
            if (!$valuerds || $timerds < 30) {
                $coinorder = (new Order())
                    ->order('clostime desc')
                    ->field('transnum,transprice,clostime')
                    ->where('clostime', '>=', date('Y-m-d H:i', (time() - $time * $size)))
                    ->where(['coin' => $lists['coin'], 'mart' => $lists['mart'], 'status' => 2])
                    ->select();
                $zerotime = strtotime(date('Y-m-d H:i')); //整点时间戳
                $data = [];
                for ($i = 0; $i < $size; $i++) {
                    $startime = $zerotime - ($i + 1) * $time;
                    $endtime = $zerotime - ($i) * $time;
                    $data[$i] = [
                        'id' => date('Y-m-d H:i', $startime),
                        'open' => 0,
                        'close' => 0,
                        'low' => 0,
                        'high' => 0,
                        'vol' => 0,
                    ];
                    $res = [];
                    for ($c = 0; $c < count($coinorder); $c++) {
                        if (strtotime($coinorder[$c]['clostime']) >= $startime && strtotime($coinorder[$c]['clostime']) <= $endtime) {
                            $data[$i]['open'] = $coinorder[$c]['transprice'];
                            if ($data[$i]['close'] == 0) {
                                $data[$i]['close'] = $coinorder[$c]['transprice'];
                            }
                            if ($data[$i]['low'] == 0 || $data[$i]['low'] > $coinorder[$c]['transprice']) {
                                $data[$i]['low'] = $coinorder[$c]['transprice'];
                            }
                            if ($data[$i]['high'] == 0 || $data[$i]['high'] < $coinorder[$c]['transprice']) {
                                $data[$i]['high'] = $coinorder[$c]['transprice'];
                            }
                            $data[$i]['vol'] += $coinorder[$c]['transnum'];
                        }
                    }
                }
                Redis::setex($rdscoinmart, $rdstimes, serialize($data));
            }
        }
    }
}
